package com.qxsx.midi.config;


import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.toolbox.Volley;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Joney on 2015/8/31.
 */

public class AppContext extends Application {
    private static AppContext mInstance;
    private static Context context;
    private double mLatitude;// 记录经度
    private double mLongtitude;// 记录纬度

    public static AppContext getInstance() {
        return mInstance;
    }

    String city_id;
    String city_name;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        context = getApplicationContext();
        inItSetting();
    }


    public static Context getContext() {
        return context;
    }

    /**
     * 初始化全局数据
     */
    private void inItSetting() {
        // 初始化 网络
        MyNet.requestQueue = Volley.newRequestQueue(context);
        BDLocationListener mListener = new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation location) {
                mLatitude = location.getLatitude();
                mLongtitude = location.getLongitude();
                dingwei();
                if (location.getLocType() == BDLocation.TypeNetWorkException) {
                    Toast.makeText(AppContext.getContext(), "网络不同导致定位失败，请检查网络是否通畅", Toast.LENGTH_SHORT).show();
                } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
                    Toast.makeText(AppContext.getContext(), "无法获取有效定位依据导致定位失败，一般是由于手机的原因，处于飞行模式下一般会造成这种结果，可以试着重启手机", Toast.LENGTH_SHORT).show();

                }
            }
        };
        final LocationClient mLocationClient = new LocationClient(context);
        mLocationClient.registerLocationListener(mListener);
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span = 1000;
        option.setScanSpan(0);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(false);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIsNeedLocationDescribe(false);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认false，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        mLocationClient.setLocOption(option);
        mLocationClient.start();


    }

    /**
     * 定位
     */
    private void dingwei() {
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        try {
            object.put("longitude", mLongtitude + "");
            object.put("latitude", mLatitude + "");
            Log.e("mLongtitude", mLongtitude + "");
            Log.e("mLatitude", mLatitude + "");
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("202", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if (array == null) {
                    return;
                } else {
                    try {
                        JSONObject object1 = array.getJSONObject(0);
                        city_id = object1.getString("id");
                        city_name = object1.getString("name");
                        Log.e("city_id", city_id);
                        Log.e("city_name", city_name);
                        if (city_id.equals("")||city_name.equals("")){
                            city_id = "110100";
                            city_name = "北京市";
                        }
                        SPUtils.put(context, "city_id", city_id);//当前城市id
                        SPUtils.put(context, "city_name", city_name);//当前城市名称
                        SPUtils.put(context, "apply_city_id", city_id);//报名考试id
                        SPUtils.put(context, "apply_city_name", city_name);//报名考试id
                        SPUtils.put(context,"chooes_city_id",city_id);//学音乐id
                        SPUtils.put(context,"chooes_city_name",city_name);//学音乐name
                        SPUtils.put(context,"miditeacher_city_id",city_id);//midi先生搜索
                        SPUtils.put(context,"miditeacher_city_name",city_name);//midi先生搜索
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }
        });

    }

}
